package com.shoujifeng.snowmusic.player;

import java.util.ArrayList;
import java.util.HashMap;

import android.app.Activity;
import android.content.Intent;
import android.content.SharedPreferences;
import android.hardware.Sensor;
import android.hardware.SensorEvent;
import android.hardware.SensorEventListener;
import android.net.Uri;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.preference.PreferenceManager;
import android.support.v4.view.ViewPager;
import android.support.v4.view.ViewPager.OnPageChangeListener;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.Window;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.AdapterView.OnItemSelectedListener;
import android.widget.Gallery;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;

import com.shoujifeng.snowmusic.player.R;
import com.shoujifeng.snowmusic.adapter.GalleryAdapter;
import com.shoujifeng.snowmusic.adapter.ListAdapter;
import com.shoujifeng.snowmusic.adapter.LoaderAdapter;
import com.shoujifeng.snowmusic.adapter.MyPagerAdapter;
import com.shoujifeng.snowmusic.adapter.OnlineListAdapter;
import com.shoujifeng.snowmusic.adapter.adapt;
import com.shoujifeng.snowmusic.player.http.GlobalValue;
import com.shoujifeng.snowmusic.player.http.ServerAccess;
import com.shoujifeng.snowmusic.player.utils.HorizontalListView;
import com.shoujifeng.snowmusic.player.utils.LoadMask;
import com.shoujifeng.snowmusic.player.utils.TabHostAnim;

public class OnlinePlayerActivity extends Activity implements SensorEventListener{
	private RelativeLayout manPlayerLay;
	private RelativeLayout womanPlayerLay;
	private RelativeLayout allPlayerLay;
	private Gallery mGallery;
	private GalleryAdapter mGalleryAdapter;
	private ArrayList<HashMap<String, Object>> mList; 
	private ArrayList<HashMap<String, Object>> mAdList;
	private ArrayList<HashMap<String, Object>> mPlayerList; // 歌手推荐
	private ServerAccess serverAccess;
	private Thread accThread;
	private LoaderAdapter mAdapter;
	private HorizontalListView mListView;
	private final int UPDATE_THREE = 3;
	private final int UPDATE_ONE = 1;
	private LinearLayout linearLayout1;
	private LinearLayout linearLayout;
	
	private View imageView;
	
	private LoadMask mLoadMask;

	@Override
	public void onCreate(Bundle savedInstanceState) {
		requestWindowFeature(Window.FEATURE_NO_TITLE);
		super.onCreate(savedInstanceState);
		setContentView(R.layout.online_player);
		init();
		initGlobalValue();
		initDate();
		mLoadMask = new LoadMask(this);
		mLoadMask.startLoad();
		accThread = new Thread(runnable);
		accThread.start();
	}

	private void init() {
		serverAccess = new ServerAccess(OnlinePlayerActivity.this);
		mListView = (HorizontalListView) findViewById(R.id.listview);
		initDate();
		manPlayerLay = (RelativeLayout) findViewById(R.id.menPlayer);
		womanPlayerLay = (RelativeLayout) findViewById(R.id.womanPlayer);
		allPlayerLay = (RelativeLayout) findViewById(R.id.manAndWoman);
		manPlayerLay.setOnClickListener(onClickListener);
		womanPlayerLay.setOnClickListener(onClickListener);
		allPlayerLay.setOnClickListener(onClickListener);
		
		imageView = findViewById(R.id.pageView);
	
		mListView.setOnItemClickListener(new OnItemClickListener() {

			@Override
			public void onItemClick(AdapterView<?> arg0, View arg1, int arg2,
					long arg3) {
				// TODO Auto-generated method stub
				HashMap< String, Object> map = new HashMap<String, Object>();
				map = mPlayerList.get(arg2);
				Intent intent = new Intent(OnlinePlayerActivity.this,OnlinePlayerContentListActivity.class);
				intent.putExtra(ServerAccess.SINGER, map.get(ServerAccess.SINGER).toString());
				intent.putExtra(ServerAccess.INDEX, map.get(ServerAccess.INDEX).toString());
				startActivity(intent);
				
				TabHostAnim.set(R.anim.right_in, R.anim.no_move);
			}
		});
	}
	
	private void creatImg(){
		linearLayout1 = (LinearLayout) imageView.findViewById(R.id.linearLayout1);
		for(int i = 0;i<mAdList.size();i++){
			linearLayout = new LinearLayout(OnlinePlayerActivity.this);
			ImageView image = new ImageView(OnlinePlayerActivity.this);
			linearLayout.setPadding(5, 5, 5, 5);	
			if(i==0){
				image.setBackgroundResource(R.drawable.no);
			}
			else{
				image.setBackgroundResource(R.drawable.on);
			}
			linearLayout.addView(image);
			
			linearLayout1.addView(linearLayout);
		}
	}
	// 设定画廊的显示方式和内容
		public void prepare() {
			
			mGallery = (Gallery) imageView.findViewById(R.id.home_gallery);
			mGalleryAdapter = new GalleryAdapter(this,mAdList);
			mGallery.setAdapter(mGalleryAdapter);
			mGallery.setOnItemClickListener(new OnItemClickListener() {

				@Override
				public void onItemClick(AdapterView<?> parent, View view,
						int position, long id) {
					Intent intent = new Intent(OnlinePlayerActivity.this,OnlineAdvContentActivity.class);
					intent.putExtra(ServerAccess.ID, mAdList.get(position).get(ServerAccess.ID).toString());
					startActivity(intent);
					
					TabHostAnim.set(R.anim.right_in, R.anim.no_move);
				}

			});
			mGallery.setOnItemSelectedListener(new OnItemSelectedListener() {

				@Override
				public void onItemSelected(AdapterView<?> arg0, View arg1,
						int arg2, long arg3) {
					//mMyView.setSeletion(arg2);
					linearLayout.removeAllViews();
					linearLayout1.removeAllViews();
						for(int i = 0;i<mAdList.size();i++){
							linearLayout = new LinearLayout(OnlinePlayerActivity.this);
							ImageView image = new ImageView(OnlinePlayerActivity.this);
							linearLayout.setPadding(5, 5, 5, 5);	
							if(i==arg2){
								image.setBackgroundResource(R.drawable.no);
							}
							else{
								image.setBackgroundResource(R.drawable.on);
							}
							linearLayout.addView(image);
							
							linearLayout1.addView(linearLayout);
						}
					
				}

				@Override
				public void onNothingSelected(AdapterView<?> arg0) {

				}
			});
		}
	private void initDate() {
		mPlayerList = new ArrayList<HashMap<String, Object>>();
		String keys[] = new String[] { ServerAccess.LISTENER,
				ServerAccess.SINGER };
		mAdapter = new LoaderAdapter(this, mPlayerList, new int[] {
				R.id.count,R.id.playerName}, keys,
				new String[] { ServerAccess.ICON_URL },
				new int[] { R.id.face }, R.drawable.icon_singer_default,
				R.layout.horizontall_list);
		mListView.setAdapter(mAdapter);
	}
	private View.OnClickListener onClickListener = new OnClickListener() {

		@Override
		public void onClick(View v) {
			// TODO Auto-generated method stub
			switch (v.getId()) {
			case R.id.menPlayer: {
				Intent intent = new Intent(OnlinePlayerActivity.this,OnlinePlayerContentActivity.class);
				intent.putExtra("name","man" );
				startActivity(intent);
			}
				break;
			case R.id.womanPlayer: {
				Intent intent = new Intent(OnlinePlayerActivity.this,OnlinePlayerContentActivity.class);
				intent.putExtra("name","woman" );
				startActivity(intent);
			}
				break;
			case R.id.manAndWoman: {
				Intent intent = new Intent(OnlinePlayerActivity.this,OnlinePlayerContentActivity.class);
				intent.putExtra("name","manAndwoman" );
				startActivity(intent);
			}
				break;
			default:
				break;
			}
			
			TabHostAnim.set(R.anim.right_in, R.anim.no_move);
		}
	};

	@Override
	public void onAccuracyChanged(Sensor arg0, int arg1) {
		// TODO Auto-generated method stub
		
	}

	@Override
	public void onSensorChanged(SensorEvent arg0) {
		// TODO Auto-generated method stub
		
	}
	
	protected Handler handler = new Handler() {
		@SuppressWarnings("unchecked")
		@Override
		public void handleMessage(Message msg) {

			switch (msg.what) {
			case UPDATE_THREE:{
				mLoadMask.stopLoad();
				updateArrayList();
			}	
			break;
			case UPDATE_ONE:{
				creatImg();
				prepare();
			}
				break;
			default:
				break;
			}
			super.handleMessage(msg);
		}
	};
	// 创建多线程获取数据
		protected Runnable runnable = new Runnable() {

			@Override
			public void run() {
				// TODO Auto-generated method stub
				try {
						String keys[] = new String[] { ServerAccess.LISTENER,
								ServerAccess.SINGER, ServerAccess.ICON_URL,
								ServerAccess.INDEX };
						mList = new ArrayList<HashMap<String,Object>>();
						mList =serverAccess.getPlayerListAtFirstPage(keys);
						sendMessage(UPDATE_THREE);
						mAdList = new ArrayList<HashMap<String,Object>>();
						String keys2[] = new String[] {ServerAccess.ID,ServerAccess.MID,ServerAccess.STATE,ServerAccess.ICON_URL,ServerAccess.CODE};
						mAdList = serverAccess.getListAdv("4",keys2);
						sendMessage(UPDATE_ONE);
					
				} catch (Exception e) {
					// TODO: handle exception
					e.printStackTrace();
				}
			}
		};
		// 发送消息到Handler
		private void sendMessage(int what) {
			Message msg = new Message();
			msg.what = what;
			handler.sendMessage(msg);
		}
		// 若在线程中更新与Adapter的动态数组将会产生致命错误。
		private void updateArrayList() {

			if (mPlayerList != null) {
				int count = mList.size();
				for (int i = 0; i < count; i++) {
					mPlayerList.add(mList.get(i));
				}
			}

			mAdapter.notifyDataSetChanged();
		}
		private void initGlobalValue() {
			GlobalValue.initSDAddr();

			GlobalValue.addUrl(ServerAccess.MAIN,
					"http://www.snowmusic.com.cn/mobile_api/main.php");
		}
}
